﻿using System.Collections.Generic;
using System.Linq;
using MvcContrib.Pagination;

namespace Sharp.Framework
{
    /// <summary>
    /// 分页列表类扩展方法类
    /// </summary>
    public static class Pagination
    {
        #region IQueryable<T> extensions

        /// <summary>
        /// 数据源转化为分页列表类
        /// </summary>
        /// <typeparam name="T">元素类型</typeparam>
        /// <param name="source">数据源</param>
        /// <param name="index">页码</param>
        /// <param name="pageSize">页面大小</param>
        /// <returns>分页列表</returns>
        public static IPagination<T> ToPagedList<T>(this IQueryable<T> source, int index, int pageSize)
        {
            return new CustomPagination<T>(source.Skip((index - 1)*pageSize).Take(pageSize).ToList(), index, pageSize,
                                           source.Count());
        }

        /// <summary>
        /// 数据源转化分页列表(页面大小默认为10)
        /// </summary>
        /// <typeparam name="T">元素类型</typeparam>
        /// <param name="source">数据源</param>
        /// <param name="index">页码</param>
        /// <returns>分页列表</returns>
        public static IPagination<T> ToPagedList<T>(this IQueryable<T> source, int index)
        {
            const int pageSize = 10;
            return new CustomPagination<T>(source.Skip((index - 1)*pageSize).Take(pageSize).ToList(), index, pageSize,
                                           source.Count());
        }

        #endregion

        #region IEnumerable<T> extensions


        /// <summary>
        /// 数据源转化为分页列表类
        /// </summary>
        /// <typeparam name="T">元素类型</typeparam>
        /// <param name="source">数据源</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="totalCount">总记录数</param>
        /// <returns>分页列表</returns>
        public static IPagination<T> ToPagedList<T>(this IEnumerable<T> source, int pageIndex, int pageSize,
                                                    int totalCount)
        {
            return new CustomPagination<T>(source, totalCount, pageIndex, pageSize);
        }

        #endregion
    }
}
